@{
	ViewData["Title"] = "生产报表";
	Layout = "~/Views/Shared/_Layout.cshtml";
}

<style>
	.search-box {
		background: white;
		padding: 20px;
		border-radius: 8px;
		box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
		margin-bottom: 20px;
	}

	.data-table {
		background: white;
		border-radius: 8px;
		box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
		overflow: hidden;
	}

	.table-header {
		background: #f8f9fa;
		padding: 15px 20px;
		border-bottom: 1px solid #dee2e6;
		font-weight: bold;
		color: #495057;
	}

	.layui-table {
		margin: 0;
	}

	.layui-table th {
		background: #f8f9fa;
		font-weight: 600;
		color: #495057;
		text-align: center;
	}

	.layui-table td {
		text-align: center;
		padding: 10px;
		vertical-align: middle;
	}

	.status-badge {
		padding: 4px 12px;
		border-radius: 12px;
		font-size: 12px;
		font-weight: bold;
		white-space: nowrap;
	}

	.status-not-started { background: #f8d7da; color: #721c24; }
	.status-in-progress { background: #d1ecf1; color: #0c5460; }
	.status-completed { background: #d4edda; color: #155724; }
	.status-paused { background: #fff3cd; color: #856404; }

	.progress-bar {
		width: 100%;
		height: 8px;
		background: #e9ecef;
		border-radius: 4px;
		overflow: hidden;
		position: relative;
	}

	.progress-fill {
		height: 100%;
		background: linear-gradient(90deg, #28a745, #20c997);
		border-radius: 4px;
		transition: width 0.3s ease;
	}

	.progress-text {
		font-size: 12px;
		margin-top: 2px;
		color: #6c757d;
	}

	.layui-form-item {
		margin-bottom: 0;
	}

	.search-controls-grid {
		display: flex;
		flex-direction: column;
		gap: 15px;
	}

	.search-row {
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
		gap: 15px;
		align-items: end;
	}

	.search-item {
		display: flex;
		flex-direction: column;
		gap: 5px;
	}

	.search-item label {
		font-size: 12px;
		color: #666;
		font-weight: 500;
		margin: 0;
	}

	.search-item .layui-input,
	.search-item select {
		width: 100%;
		padding: 8px 12px;
		border: 1px solid #dcdee2;
		border-radius: 4px;
		font-size: 14px;
	}

	.search-buttons {
		flex-direction: row !important;
		gap: 10px !important;
		align-items: center;
		justify-content: flex-start;
	}

	.search-buttons button {
		margin: 0;
		white-space: nowrap;
	}

	@@media (max-width: 768px) {
		.search-row {
			grid-template-columns: 1fr;
		}
		
		.search-buttons {
			justify-content: center;
		}
	}

	.pagination-info {
		padding: 15px 20px;
		background: #f8f9fa;
		border-top: 1px solid #dee2e6;
		display: flex;
		justify-content: between;
		align-items: center;
	}

	.loading {
		text-align: center;
		padding: 40px;
		color: #6c757d;
	}

	.advanced-search-header {
		background: white;
		padding: 15px 20px;
		border-radius: 8px;
		box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
		margin-bottom: 10px;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.advanced-search-btn {
		background: #1890ff;
		color: white;
		border: none;
		padding: 8px 20px;
		border-radius: 4px;
		cursor: pointer;
		font-size: 14px;
		display: flex;
		align-items: center;
		gap: 5px;
		transition: all 0.3s ease;
	}

	.advanced-search-btn:hover {
		background: #40a9ff;
		transform: translateY(-1px);
	}

	.advanced-search-btn i {
		transition: transform 0.3s ease;
	}

	.advanced-search-btn.expanded i {
		transform: rotate(180deg);
	}

	.search-controls-container {
		max-height: 0;
		overflow: hidden;
		transition: max-height 0.3s ease-out;
	}

	.search-controls-container.expanded {
		max-height: 1000px;
		transition: max-height 0.5s ease-in;
	}

	.search-title {
		font-size: 16px;
		font-weight: 600;
		color: #333;
		margin: 0;
	}
</style>

<div class="layui-container">
	<div class="layui-row">
		<div class="layui-col-md12">
			<!-- 高级查询头部 -->
			<div class="advanced-search-header">
				<div class="search-title">生产报表</div>
				<button type="button" class="advanced-search-btn" onclick="toggleAdvancedSearch()">
					<i class="layui-icon layui-icon-down"></i>
					<span id="advancedSearchText">高级查询</span>
				</button>
			</div>
			
			<!-- 搜索区域 -->
			<div class="search-box">
				<div class="search-controls-container" id="searchControlsContainer">
					<div class="search-controls-grid">
					<!-- 第一行 -->
					<div class="search-row">
						<div class="search-item">
							<label>工单编号:</label>
							<input type="text" id="workOrderCodeFilter" placeholder="工单编号" class="layui-input">
						</div>
						<div class="search-item">
							<label>产品编号:</label>
							<input type="text" id="productCodeFilter" placeholder="产品编号" class="layui-input">
						</div>
						<div class="search-item">
							<label>产品名称:</label>
							<input type="text" id="productNameFilter" placeholder="产品名称" class="layui-input">
						</div>
						<div class="search-item">
							<label>工序编号:</label>
							<input type="text" id="processCodeFilter" placeholder="工序编号" class="layui-input">
						</div>
					</div>
					
					<!-- 第二行 -->
					<div class="search-row">
						<div class="search-item">
							<label>工序名称:</label>
							<input type="text" id="processNameFilter" placeholder="工序名称" class="layui-input">
						</div>
						<div class="search-item">
							<label>工单状态:</label>
							<select id="workOrderStatusFilter" lay-search="">
								<option value="">全部工单状态</option>
								<option value="未开始">未开始</option>
								<option value="进行中">进行中</option>
								<option value="已完成">已完成</option>
								<option value="已暂停">已暂停</option>
							</select>
						</div>
						<div class="search-item">
							<label>工序状态:</label>
							<select id="processStatusFilter" lay-search="">
								<option value="">全部工序状态</option>
								<option value="NotStarted">未开始</option>
								<option value="InProgress">进行中</option>
								<option value="Completed">已完成</option>
								<option value="Paused">已暂停</option>
							</select>
						</div>
						<div class="search-item">
							<label>计划开始时间:</label>
							<input type="date" id="plannedStartTimeStart" class="layui-input">
						</div>
					</div>
					
					<!-- 第三行 -->
					<div class="search-row">
						<div class="search-item">
							<label>计划开始时间(结束):</label>
							<input type="date" id="plannedStartTimeEnd" class="layui-input">
						</div>
						<div class="search-item">
							<label>计划结束时间:</label>
							<input type="date" id="plannedEndTimeStart" class="layui-input">
						</div>
						<div class="search-item">
							<label>计划结束时间(结束):</label>
							<input type="date" id="plannedEndTimeEnd" class="layui-input">
						</div>
						<div class="search-item search-buttons">
							<button type="button" class="layui-btn" onclick="searchWorkOrders()">
								<i class="layui-icon layui-icon-search"></i> 搜索
							</button>
							<button type="button" class="layui-btn layui-btn-primary" onclick="resetSearch()">
								<i class="layui-icon layui-icon-refresh"></i> 重置
							</button>
						</div>
					</div>
					</div>
				</div>
			</div>

			<!-- 数据表格 -->
			<div class="data-table">
				<div class="table-header">
					工单综合信息数据
				</div>
				<table class="layui-table" lay-size="sm">
					<thead>
						<tr>
							<th>工单编号</th>
							<th>产品编号</th>
							<th>产品名称</th>
							<th>规格</th>
							<th>单位</th>
							<th>计划数量</th>
							<th>实际数量</th>
							<th>良品数量</th>
							<th>不良品数量</th>
							<th>完成进度</th>
							<th>工序进度</th>
							<th>当前工序</th>
							<th>状态</th>
						</tr>
					</thead>
					<tbody id="workOrderTableBody">
						<tr>
							<td colspan="13" class="loading">
								<i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i>
								加载中...
							</td>
						</tr>
					</tbody>
				</table>
				
				<!-- 分页和统计信息 -->
				<div class="pagination-info">
					<div>
						共 <span id="totalRecords">0</span> 条记录
					</div>
					<div id="pagination"></div>
				</div>
			</div>
		</div>
	</div>
</div>

@section Scripts {
<script>
$(document).ready(function() {
	console.log('====== 页面开始初始化 ======');
	console.log('jQuery版本:', $.fn.jquery);
	console.log('ViewBag配置:');
	console.log('  - ReadApiUrl:', '@ViewBag.ReadApiUrl');
	console.log('  - WriteApiUrl:', '@ViewBag.WriteApiUrl');
	console.log('  - Token存在:', '@ViewBag.Token' ? true : false);
	console.log('  - Token长度:', '@ViewBag.Token'.length);
	console.log('  - Token原始值:', '@ViewBag.Token');
	
	// 检查必要的配置
	const readApiUrl = '@ViewBag.ReadApiUrl';
	const token = '@ViewBag.Token';
	
	if (!readApiUrl || readApiUrl === '') {
		console.error('ReadApiUrl未配置！');
		showError('API配置错误：ReadApiUrl未配置');
		return;
	}
	
	if (!token || token === '') {
		console.warn('Token未配置，可能需要登录');
		// 不直接返回，尝试无Token调用
	}
	
	// 初始化
	initializePage();
	loadWorkOrders();
	
	// 初始化高级查询为折叠状态
	initializeAdvancedSearch();
});

// 页面配置
const pageConfig = {
	currentPage: 1,
	pageSize: 20,
	totalRecords: 0,
	allWorkOrders: [],
	filteredWorkOrders: []
};

// 初始化页面
function initializePage() {
	// 初始化下拉框
	layui.use(['form'], function() {
		const form = layui.form;
		form.render('select');
		
		// 下拉框变化事件
		form.on('select()', function(data) {
			// 可以选择是否在下拉框变化时自动搜索，这里暂时注释掉
			// searchWorkOrders();
		});
	});

	// 为所有文本输入框添加回车事件
	const textInputs = [
		'#workOrderCodeFilter', '#productCodeFilter', '#productNameFilter',
		'#processCodeFilter', '#processNameFilter'
	];
	
	textInputs.forEach(selector => {
		$(selector).on('keypress', function(e) {
			if (e.which === 13) {
				searchWorkOrders();
			}
		});
	});

	// 为日期输入框添加变化事件
	const dateInputs = [
		'#plannedStartTimeStart', '#plannedStartTimeEnd',
		'#plannedEndTimeStart', '#plannedEndTimeEnd'
	];
	
	dateInputs.forEach(selector => {
		$(selector).on('change', function() {
			// 可以选择是否在日期变化时自动搜索，这里暂时注释掉
			// searchWorkOrders();
		});
	});
}

// 加载工单综合信息数据
async function loadWorkOrders() {
	try {
		showLoading();
		
		// 添加详细的调试信息
		const apiUrl = `@ViewBag.ReadApiUrl/WorkOrder/comprehensive/all`;
		const token = `@ViewBag.Token`;
		
		console.log('====== 开始调用API ======');
		console.log('API URL:', apiUrl);
		console.log('Token:', token ? '有Token' : '无Token');
		console.log('Token长度:', token ? token.length : 0);
		
		// 构建请求头
		const headers = {
			'Content-Type': 'application/json'
		};
		
		// 只有在有Token时才添加Authorization头
		if (token && token.trim() !== '') {
			headers['Authorization'] = 'Bearer ' + token;
			console.log('使用Token进行授权请求');
		} else {
			console.log('无Token，尝试无授权请求');
		}
		
		// 获取工单综合信息列表
		const workReportResponse = await fetch(apiUrl, {
			method: 'GET',
			headers: headers
		});

		console.log('响应状态:', workReportResponse.status);
		console.log('响应状态文本:', workReportResponse.statusText);
		console.log('响应头:', [...workReportResponse.headers.entries()]);

		if (!workReportResponse.ok) {
			const errorText = await workReportResponse.text();
			console.error('API错误响应:', errorText);
			throw new Error(`获取工作报告失败: ${workReportResponse.status} - ${errorText}`);
		}

		const response = await workReportResponse.json();
		console.log('====== API响应成功 ======');
		console.log('完整响应:', response);
		console.log('响应类型:', typeof response);
		console.log('响应结构:');
		console.log('  - response.code:', response.code);
		console.log('  - response.msg:', response.msg);
		console.log('  - response.data:', response.data);
		if (response.data) {
			console.log('  - response.data类型:', typeof response.data);
			console.log('  - response.data.data:', response.data.data);
			if (response.data.data) {
				console.log('  - response.data.data长度:', response.data.data.length);
				console.log('  - response.data.data内容:', response.data.data);
			}
		}
		
		// 检查响应格式
		let workOrders = [];
		console.log('====== 开始解析数据 ======');
		
		// 检查API是否成功返回
		if (response && response.code === 200) {
			if (Array.isArray(response.data)) {
				workOrders = response.data;
				console.log('使用格式: response.data，数据长度:', workOrders.length);
			} else {
				console.warn('API返回成功但data不是数组:', response.data);
			}
		} else if (response && response.data && Array.isArray(response.data.data)) {
			workOrders = response.data.data;
			console.log('使用格式: response.data.data，数据长度:', workOrders.length);
		} else if (response && Array.isArray(response.data)) {
			workOrders = response.data;
			console.log('使用格式: response.data，数据长度:', workOrders.length);
		} else if (Array.isArray(response)) {
			workOrders = response;
			console.log('使用格式: response，数据长度:', workOrders.length);
		} else {
			console.warn('未能识别的数据格式，response:', response);
			if (response) {
				console.log('响应code:', response.code);
				console.log('响应msg:', response.msg);
			}
		}

		console.log('====== 解析完成 ======');
		console.log('最终工单综合信息数据:', workOrders);
		console.log('数据数量:', workOrders.length);
		
		if (workOrders.length > 0) {
			console.log('第一条数据示例:', workOrders[0]);
			console.log('数据字段:', Object.keys(workOrders[0]));
		}

		pageConfig.allWorkOrders = workOrders;
		pageConfig.filteredWorkOrders = workOrders;
		pageConfig.totalRecords = workOrders.length;
		
		console.log('====== 更新页面显示 ======');
		console.log('pageConfig.totalRecords:', pageConfig.totalRecords);
		
		updateDisplay();
		
	} catch (error) {
		console.error('====== API调用失败 ======');
		console.error('错误详情:', error);
		console.error('错误消息:', error.message);
		console.error('错误堆栈:', error.stack);
		showError('加载数据失败，请检查网络连接或重新登录。详细错误: ' + error.message);
	}
}

// 搜索工单综合信息
function searchWorkOrders() {
	// 获取所有搜索条件
	const workOrderCode = $('#workOrderCodeFilter').val().toLowerCase().trim();
	const productCode = $('#productCodeFilter').val().toLowerCase().trim();
	const productName = $('#productNameFilter').val().toLowerCase().trim();
	const processCode = $('#processCodeFilter').val().toLowerCase().trim();
	const processName = $('#processNameFilter').val().toLowerCase().trim();
	const workOrderStatus = $('#workOrderStatusFilter').val();
	const processStatus = $('#processStatusFilter').val();
	const plannedStartTimeStart = $('#plannedStartTimeStart').val();
	const plannedStartTimeEnd = $('#plannedStartTimeEnd').val();
	const plannedEndTimeStart = $('#plannedEndTimeStart').val();
	const plannedEndTimeEnd = $('#plannedEndTimeEnd').val();
	
	console.log('搜索条件:', {
		workOrderCode, productCode, productName, processCode, processName,
		workOrderStatus, processStatus, plannedStartTimeStart, plannedStartTimeEnd,
		plannedEndTimeStart, plannedEndTimeEnd
	});
	
	pageConfig.filteredWorkOrders = pageConfig.allWorkOrders.filter(item => {
		// 工单编号筛选
		const matchesWorkOrderCode = !workOrderCode || 
			(item.workOrder?.code && item.workOrder.code.toLowerCase().includes(workOrderCode));
		
		// 产品编号筛选
		const matchesProductCode = !productCode || 
			(item.product?.productCode && item.product.productCode.toLowerCase().includes(productCode));
		
		// 产品名称筛选
		const matchesProductName = !productName || 
			(item.product?.productName && item.product.productName.toLowerCase().includes(productName));
		
		// 工序编号筛选
		const matchesProcessCode = !processCode || 
			(item.process?.processCode && item.process.processCode.toLowerCase().includes(processCode));
		
		// 工序名称筛选
		const matchesProcessName = !processName || 
			(item.process?.processName && item.process.processName.toLowerCase().includes(processName));
		
		// 工单状态筛选 (这里可能需要根据实际数据结构调整)
		let workOrderMappedStatus = '';
		switch(item.workOrder?.status) {
			case 'Completed': 
			case '已完成': workOrderMappedStatus = '已完成'; break;
			case 'InProgress': 
			case '进行中': workOrderMappedStatus = '进行中'; break;
			case 'NotStarted': 
			case '未开始': workOrderMappedStatus = '未开始'; break;
			case 'Paused': 
			case '已暂停': workOrderMappedStatus = '已暂停'; break;
			default: workOrderMappedStatus = item.workOrder?.status || ''; break;
		}
		const matchesWorkOrderStatus = !workOrderStatus || workOrderMappedStatus === workOrderStatus;
		
		// 工序状态筛选
		const matchesProcessStatus = !processStatus || item.process?.status === processStatus;
		
		// 计划开始时间筛选
		let matchesPlannedStartTime = true;
		if (plannedStartTimeStart || plannedStartTimeEnd) {
			const itemStartTime = item.workOrder?.plannedStartTime || item.process?.plannedStartTime;
			if (itemStartTime) {
				const itemDate = new Date(itemStartTime).toISOString().split('T')[0];
				if (plannedStartTimeStart && itemDate < plannedStartTimeStart) {
					matchesPlannedStartTime = false;
				}
				if (plannedStartTimeEnd && itemDate > plannedStartTimeEnd) {
					matchesPlannedStartTime = false;
				}
			} else {
				matchesPlannedStartTime = false;
			}
		}
		
		// 计划结束时间筛选
		let matchesPlannedEndTime = true;
		if (plannedEndTimeStart || plannedEndTimeEnd) {
			const itemEndTime = item.workOrder?.plannedEndTime || item.process?.plannedEndTime;
			if (itemEndTime) {
				const itemDate = new Date(itemEndTime).toISOString().split('T')[0];
				if (plannedEndTimeStart && itemDate < plannedEndTimeStart) {
					matchesPlannedEndTime = false;
				}
				if (plannedEndTimeEnd && itemDate > plannedEndTimeEnd) {
					matchesPlannedEndTime = false;
				}
			} else {
				matchesPlannedEndTime = false;
			}
		}
		
		return matchesWorkOrderCode && matchesProductCode && matchesProductName && 
			   matchesProcessCode && matchesProcessName && matchesWorkOrderStatus && 
			   matchesProcessStatus && matchesPlannedStartTime && matchesPlannedEndTime;
	});
	
	pageConfig.currentPage = 1;
	pageConfig.totalRecords = pageConfig.filteredWorkOrders.length;
	updateDisplay();
	
	console.log(`筛选结果: ${pageConfig.filteredWorkOrders.length}/${pageConfig.allWorkOrders.length} 条记录`);
}

// 重置搜索
function resetSearch() {
	// 清空所有搜索字段
	$('#workOrderCodeFilter').val('');
	$('#productCodeFilter').val('');
	$('#productNameFilter').val('');
	$('#processCodeFilter').val('');
	$('#processNameFilter').val('');
	$('#workOrderStatusFilter').val('');
	$('#processStatusFilter').val('');
	$('#plannedStartTimeStart').val('');
	$('#plannedStartTimeEnd').val('');
	$('#plannedEndTimeStart').val('');
	$('#plannedEndTimeEnd').val('');
	
	// 重新渲染下拉框
	layui.use(['form'], function() {
		layui.form.render('select');
	});
	
	// 重置筛选结果
	pageConfig.filteredWorkOrders = pageConfig.allWorkOrders;
	pageConfig.currentPage = 1;
	pageConfig.totalRecords = pageConfig.allWorkOrders.length;
	updateDisplay();
	
	console.log('已重置所有搜索条件');
}

// 初始化高级查询
function initializeAdvancedSearch() {
	const container = document.getElementById('searchControlsContainer');
	const btn = document.querySelector('.advanced-search-btn');
	const text = document.getElementById('advancedSearchText');
	
	// 默认为折叠状态
	container.classList.remove('expanded');
	btn.classList.remove('expanded');
	text.textContent = '高级查询';
	
	console.log('高级查询已初始化为折叠状态');
}

// 切换高级查询显示状态
function toggleAdvancedSearch() {
	const container = document.getElementById('searchControlsContainer');
	const btn = document.querySelector('.advanced-search-btn');
	const text = document.getElementById('advancedSearchText');
	const icon = btn.querySelector('i');
	
	if (container.classList.contains('expanded')) {
		// 当前是展开状态，要折叠
		container.classList.remove('expanded');
		btn.classList.remove('expanded');
		text.textContent = '高级查询';
		icon.className = 'layui-icon layui-icon-down';
		console.log('高级查询已折叠');
	} else {
		// 当前是折叠状态，要展开
		container.classList.add('expanded');
		btn.classList.add('expanded');
		text.textContent = '收起查询';
		icon.className = 'layui-icon layui-icon-up';
		console.log('高级查询已展开');
	}
}

// 更新显示
function updateDisplay() {
	renderWorkOrderTable();
	renderPagination();
	updateStatistics();
}

// 渲染工单综合信息表格
function renderWorkOrderTable() {
	const tbody = $('#workOrderTableBody');
	const startIndex = (pageConfig.currentPage - 1) * pageConfig.pageSize;
	const endIndex = startIndex + pageConfig.pageSize;
	const pageData = pageConfig.filteredWorkOrders.slice(startIndex, endIndex);
	
	if (pageData.length === 0) {
		tbody.html('<tr><td colspan="13" class="loading">暂无数据</td></tr>');
		return;
	}
	
	const rows = pageData.map(item => {
		// 使用新的数据结构获取数据
		const workOrder = item.workOrder || {};
		const process = item.process || {};
		const product = item.product || {};
		const progress = item.progress || {};
		
		// 计算工序完成进度
		const processCompletionPercentage = progress.processCompletionPercentage || 0;
		const workOrderCompletionPercentage = progress.workOrderCompletionPercentage || 0;
		
		// 获取数量信息
		const plannedQty = process.plannedQuantity || workOrder.plannedQuantity || 0;
		const reportedQty = process.reportedQuantity || 0;
		const goodQty = process.goodQuantity || 0;
		const badQty = process.badQuantity || 0;
		const completedQty = reportedQty;
		
		// 映射状态
		let displayStatus = '';
		switch(process.status) {
			case 'Completed': 
			case '已完成': displayStatus = '已完成'; break;
			case 'InProgress': 
			case '进行中': displayStatus = '进行中'; break;
			case 'NotStarted': 
			case '未开始': displayStatus = '未开始'; break;
			case 'Paused': 
			case '已暂停': displayStatus = '已暂停'; break;
			default: displayStatus = process.status || '未知'; break;
		}
		
		return `
			<tr>
				<td>${workOrder.code || '-'}</td>
				<td>${product.productCode || '-'}</td>
				<td>${product.productName || '-'}</td>
				<td>${product.spec || '-'}</td>
				<td>${product.unit || '-'}</td>
				<td>${plannedQty || '-'}</td>
				<td>${reportedQty || '-'}</td>
				<td>${goodQty || '-'}</td>
				<td>${badQty || '-'}</td>
				<td>
					<div class="progress-bar">
						<div class="progress-fill" style="width: ${workOrderCompletionPercentage}%"></div>
					</div>
					<div class="progress-text">${workOrderCompletionPercentage.toFixed(1)}%</div>
				</td>
				<td>
					<div class="progress-bar">
						<div class="progress-fill" style="width: ${processCompletionPercentage}%"></div>
					</div>
					<div class="progress-text">${processCompletionPercentage.toFixed(1)}%</div>
				</td>
				<td>${process.processName || '-'}</td>
				<td>
					<span class="status-badge ${getStatusClass(displayStatus)}">
						${displayStatus}
					</span>
				</td>
			</tr>
		`;
	}).join('');
	
	tbody.html(rows);
}

// 获取状态样式类
function getStatusClass(status) {
	switch (status) {
		case '未开始': return 'status-not-started';
		case '进行中': return 'status-in-progress';
		case '已完成': return 'status-completed';
		case '已暂停': return 'status-paused';
		default: return 'status-not-started';
	}
}

// 渲染分页
function renderPagination() {
	layui.use(['laypage'], function() {
		const laypage = layui.laypage;
		
		laypage.render({
			elem: 'pagination',
			count: pageConfig.totalRecords,
			limit: pageConfig.pageSize,
			curr: pageConfig.currentPage,
			layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
			limits: [10, 20, 30, 50],
			jump: function(obj, first) {
				if (!first) {
					pageConfig.currentPage = obj.curr;
					pageConfig.pageSize = obj.limit;
					renderWorkOrderTable();
				}
			}
		});
	});
}

// 更新统计信息
function updateStatistics() {
	$('#totalRecords').text(pageConfig.totalRecords);
}

// 显示加载状态
function showLoading() {
	$('#workOrderTableBody').html('<tr><td colspan="13" class="loading"><i class="layui-icon layui-icon-loading layui-anim layui-anim-rotate layui-anim-loop"></i> 加载中...</td></tr>');
}

// 显示错误信息
function showError(message) {
	$('#workOrderTableBody').html(`<tr><td colspan="13" class="loading" style="color: #f56c6c;"><i class="layui-icon layui-icon-close"></i> ${message}</td></tr>`);
	
	layui.use(['layer'], function() {
		layui.layer.msg(message, {icon: 2});
	});
}
</script>
}